package sharetime.playlist.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import sharetime.constants.PermissionTypes;
import sharetime.playlist.domain.Playlist;
import sharetime.user.domain.User;
import sharetime.user.service.IPermissionDelegate;

@Service("playlistPermissionDelegate")
public class PlaylistPermissionDelegate implements IPermissionDelegate {
	@Autowired
	@Qualifier("playlistPermissionManager")
	private IPlaylistPermissionManager playlistPermissionManager;

	@Override
	public boolean canCreate(User user, Object target) {
		return hasPlaylistPermission(PermissionTypes.CREATE, user, (Playlist) target
				);
	}

	@Override
	public boolean canEdit(User user, Object target) {
		return hasPlaylistPermission(PermissionTypes.EDIT, user, (Playlist) target);
	}

	@Override
	public boolean canRemove(User user, Object target) {
		return hasPlaylistPermission(PermissionTypes.REMOVE, user, (Playlist) target);
	}

	@Override
	public boolean canSetPermissions(User user, Object target) {
		return hasPlaylistPermission(PermissionTypes.SET_PERMISSIONS,
				user, (Playlist) target);
	}

	@Override
	public boolean canView(User user, Object target) {
		return hasPlaylistPermission(PermissionTypes.VIEW, user, (Playlist) target);
	}

	private boolean hasPlaylistPermission(long permission, User user,
			Playlist playlist) {
		return playlistPermissionManager.hasPermission(permission, user,
				playlist);
	}
}
